import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Menu from '../components/Menu.vue'

Vue.use(VueRouter)

const routes = [
  // 这是登录页
  {
    path: '/',
    name: 'Login',
    component: Login,
    redirect: '/login',
    children: [
      { path: '/login', component: Login }
    ]
  },
  // 这是首页
  // children是右侧区域显示的页面
  {
    path: '/menu',
    name: 'Menu',
    component: Menu,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import(/* webpackChunkName: "about" */ '../components/Home.vue')
      },
      {
        path: '/task',
        name: 'Task',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../components/Task.vue')
      }, {
        path: '/wish',
        name: 'Wish',
        component: () => import(/* webpackChunkName: "about" */ '../components/Wish.vue')
      },
      {
        path: '/money',
        name: 'Money',
        component: () => import(/* webpackChunkName: "about" */ '../components/Money.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫：从哪里来到哪里去是否放行
router.beforeEach((to, form, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
